package com.github.alexmao86.json;

import java.math.BigInteger;

/**
 * This class holds a number value that is lazily converted to a specific number
 * type
 *
 * @author
 */
@SuppressWarnings("serial")
public final class LazilyParsedNumber extends Number {
	private final String value;

	public LazilyParsedNumber(String value) {
		this.value = value;
	}

	@Override
	public int intValue() {
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			try {
				return (int) Long.parseLong(value);
			} catch (NumberFormatException nfe) {
				return new BigInteger(value).intValue();
			}
		}
	}

	@Override
	public long longValue() {
		try {
			return Long.parseLong(value);
		} catch (NumberFormatException e) {
			return new BigInteger(value).longValue();
		}
	}

	@Override
	public float floatValue() {
		return Float.parseFloat(value);
	}

	@Override
	public double doubleValue() {
		return Double.parseDouble(value);
	}

	@Override
	public String toString() {
		return value;
	}
}